/*
 * Copyright (C) 2016 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kwan.xframe.mvvm.data.source.http.converter.scalars;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import timber.log.Timber;

final class XScalarResponseBodyConverters {
	private XScalarResponseBodyConverters() {
	}

	static final class XStringResponseBodyConverter implements Converter<ResponseBody, String> {
		static final XStringResponseBodyConverter INSTANCE = new XStringResponseBodyConverter();

		@Override
		public String convert(ResponseBody value) throws IOException {
			String str = value.string();
			Timber.e("convert String---" + str);
			return str;
		}
	}

	static final class XBooleanResponseBodyConverter implements Converter<ResponseBody, Boolean> {
		static final XBooleanResponseBodyConverter INSTANCE = new XBooleanResponseBodyConverter();

		@Override
		public Boolean convert(ResponseBody value) throws IOException {
			return Boolean.valueOf(value.string());
		}
	}

	static final class XByteResponseBodyConverter implements Converter<ResponseBody, Byte> {
		static final XByteResponseBodyConverter INSTANCE = new XByteResponseBodyConverter();

		@Override
		public Byte convert(ResponseBody value) throws IOException {
			return Byte.valueOf(value.string());
		}
	}

	static final class XCharacterResponseBodyConverter implements Converter<ResponseBody, Character> {
		static final XCharacterResponseBodyConverter INSTANCE = new XCharacterResponseBodyConverter();

		@Override
		public Character convert(ResponseBody value) throws IOException {
			String body = value.string();
			if (body.length() != 1) {
				throw new IOException(
						"Expected body of length 1 for Character conversion but was " + body.length());
			}
			return body.charAt(0);
		}
	}

	static final class XDoubleResponseBodyConverter implements Converter<ResponseBody, Double> {
		static final XDoubleResponseBodyConverter INSTANCE = new XDoubleResponseBodyConverter();

		@Override
		public Double convert(ResponseBody value) throws IOException {
			return Double.valueOf(value.string());
		}
	}

	static final class XFloatResponseBodyConverter implements Converter<ResponseBody, Float> {
		static final XFloatResponseBodyConverter INSTANCE = new XFloatResponseBodyConverter();

		@Override
		public Float convert(ResponseBody value) throws IOException {
			return Float.valueOf(value.string());
		}
	}

	static final class XIntegerResponseBodyConverter implements Converter<ResponseBody, Integer> {
		static final XIntegerResponseBodyConverter INSTANCE = new XIntegerResponseBodyConverter();

		@Override
		public Integer convert(ResponseBody value) throws IOException {
			return Integer.valueOf(value.string());
		}
	}

	static final class XLongResponseBodyConverter implements Converter<ResponseBody, Long> {
		static final XLongResponseBodyConverter INSTANCE = new XLongResponseBodyConverter();

		@Override
		public Long convert(ResponseBody value) throws IOException {
			return Long.valueOf(value.string());
		}
	}

	static final class XShortResponseBodyConverter implements Converter<ResponseBody, Short> {
		static final XShortResponseBodyConverter INSTANCE = new XShortResponseBodyConverter();

		@Override
		public Short convert(ResponseBody value) throws IOException {
			return Short.valueOf(value.string());
		}
	}
}
